*********************************************************************
* Disentangling the Relationship Between Prospective Expectations   *
* and Policy Preferences in Violent Conflicts                       *
* Alon Yakter and Liran Harsgor 									*
* British Journal of Political Science (accepted 2025)				*
*                                                                   *
* Replication code - aggregate data analysis (VAR model)			* *********************************************************************
* The analysis was performed with Stata 17. 						*
* Users should install the following packages: estout, grystyle.	*
* (command line: "ssc install [package name], replace").			*
*																	*
* to load the data (the "use" command), users should place the .dta	*
* file in their Stata working directory (or type the full file 		*
* paths as saved on their computer).                      			*
*********************************************************************

***************
* set up data *
***************
** open data **
use "Yakter & Harsgor BJPS 2025 - Aggregate Data.dta", clear
** set data as time series **
tsset ts_date, monthly

**********************
* set up graph style *
**********************
grstyle init
grstyle set plain, nogrid box
grstyle set compact
grstyle set legend, nobox klength(5)
grstyle set color black black

*********************************
* Figure 1 - descriptive graphs *
*********************************
** Descriptive plot of the time series ** 
tsline expectations if tin(2001m7, 2020m5), ///
	tlabel(2001m7(36)2020m5, format(%tmnn/CCYY)) ///
	ylabel(-60 "-60%" -40 "-40%" -20 "-20%" 0 "0%" 20 "20%") 	///
	ttitle("Date") ytitle("Aggregate Net Expectations") ///
	title("A. Aggregate Net Expectations For An Agreement")	///
	name(expectations, replace)
tsline preferences if tin(2001m7, 2020m5), ///
	lpattern(dash) ///
	tlabel(2001m7(36)2020m5, format(%tmnn/CCYY)) ///
	ylabel(0 "0%" 20 "20%" 40 "40%" 60 "60%" 80 "80%") 	///
	ttitle("Date") ytitle("Aggregate Net Preference") ///
	title("B. Aggregate Net Ideological Preferences for Negotiations")	///
	name(preferences, replace)
graph combine expectations preferences  ///
	,col(1)

*****************************************************
* Table 2 - VAR analysis and Granger causality test *
*****************************************************
* VAR estimation (quietly)
qui var expectations preferences, ///
	exog(l(0/1).log_rockets l(0/1).log_casualties_il l(0/1).log_casualties_pl ///
	l(0/1).negotiations l(0/1).hawkish_selection_pl d.right_cabinet_il) ///
	lag(1/4)
* post-etimation Granger causality
vargranger

************************
* Figure 2 - IRF graph *
************************
* VAR estimaiton (quiet; ordering based on Granger results, i.e., expectations -> preferences) **
qui var expectations preferences, ///
	exog(l(0/1).log_rockets l(0/1).log_casualties_il l(0/1).log_casualties_pl ///
	l(0/1).negotiations l(0/1).hawkish_selection_pl d.right_cabinet_il) ///
	lag(1/4)
* calculate impulse-response function (IRF)
irf create var1, set(myirf) order(expectations preferences) step(20) replace
* plot OIRF
irf graph oirf, ///
	impulse(expectations) response(preferences)  ///
	yline(0, lcolor(gray)) ci1opts(fcolor(%60) lwidth(none)) ///
	xtitle("Months after Shock", size(medlarge)) ytitle("Orthogonalized IRF", size(medlarge)) ///
	xlabel(, labsize(large)) ylabel(, labsize(large)) ///
	subtitle("") byopts(note("") legend(off)) ///
	ysize(2) xsize(3)
	

*******************************
* Table A1 -  unit root tests *
*******************************
* determining the short and long lag truncation for tests (based on Schwert's advice)
foreach v of varlist expectations preferences {
	sum `v' 
	dis "Short lag truncation: " trunc(4 * (r(N)/100)^0.25)
	dis "Long lag truncation: " trunc(12 * (r(N)/100)^0.25)
	}
* tests for unit root
foreach v of varlist expectations preferences {
	dis "`v':"
	dfuller `v', lags(1)
	dfuller `v', lags(1) trend
	pperron `v', lags(1)
	pperron `v', lags(1) trend
	lomackinlay `v', q(4 14)
	dfgls `v', notrend
	dfgls `v'
	}
* dfgls tests by variable-specific suggested lags based on first test (Ng-Perron) and varsoc (AIC, BIC)
foreach v of varlist expectations preferences {
	varsoc `v', maxlag(20)
	}
dfgls expectations, maxlag(4) notrend
dfgls expectations, maxlag(4)
dfgls expectations, maxlag(2) notrend
dfgls expectations, maxlag(2)
dfgls preferences, maxlag(3) notrend
dfgls preferences, maxlag(3)
dfgls preferences, maxlag(2) notrend
dfgls preferences, maxlag(2)

*********************************
* Table A2 -  VAR lag selection *
*********************************
varsoc expectations preferences, maxlag(12)

**************************************************
* Table A3 -  post-estimation Wald lag exclusion *
**************************************************
qui var expectations preferences, ///
	exog(l(0/1).log_rockets l(0/1).log_casualties_il l(0/1).log_casualties_pl ///
	l(0/1).negotiations l(0/1).hawkish_selection_pl d.right_cabinet_il) ///
	lag(1/4)
varwle

**********************************
* Table A4 - full VAR estimation *
**********************************
eststo clear
qui eststo: var expectations preferences, ///
	exog(l(0/1).log_rockets l(0/1).log_casualties_il l(0/1).log_casualties_pl ///
	l(0/1).negotiations l(0/1).hawkish_selection_pl d.right_cabinet_il) ///
	lag(1/4)
esttab, b(3) se(3) scalars(N r2_1 r2_2) sfmt(%9.3f) ///
	compress nogaps unstack wide 

********************************************************
* Table A5 - post-estimation diagnostics of normality *
*******************************************************
qui var expectations preferences, ///
	exog(l(0/1).log_rockets l(0/1).log_casualties_il l(0/1).log_casualties_pl ///
	l(0/1).negotiations l(0/1).hawkish_selection_pl d.right_cabinet_il) ///
	lag(1/4)
varnorm
*******************************************************
* Figure A1 - post-estimation diagnostics of stability *
*******************************************************
qui var expectations preferences, ///
	exog(l(0/1).log_rockets l(0/1).log_casualties_il l(0/1).log_casualties_pl ///
	l(0/1).negotiations l(0/1).hawkish_selection_pl d.right_cabinet_il) ///
	lag(1/4)
varstable, graph

